package com.woniuxy.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.food.infra.mapper.FoodPropMapper;
import com.woniuxy.food.infra.mapper.FoodSkuPropValueMapper;
import com.woniuxy.food.infra.mapper.FoodSpuMapper;
import com.woniuxy.food.infra.model.FoodProp;
import com.woniuxy.food.infra.model.FoodPropValue;
import com.woniuxy.food.infra.mapper.FoodPropValueMapper;
import com.woniuxy.food.infra.model.FoodSkuPropValue;
import com.woniuxy.food.infra.model.FoodSpu;
import com.woniuxy.food.service.FoodPropValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.food.service.servicedto.FoodPropValueServiceDto;
import com.woniuxy.food.service.serviceparam.InsertFoodPropServiceParam;
import com.woniuxy.food.service.serviceparam.InsertFoodPropValueServiceParam;
import exception.MallException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import java.util.List;

/**
 * <p>
 * 菜品属性值表 服务实现类
 * </p>
 *
 * @author 刘德华
 * @since 2023-07-19
 */
@Service
public class FoodPropValueServiceImpl extends ServiceImpl<FoodPropValueMapper, FoodPropValue> implements FoodPropValueService {

    @Autowired
    private FoodPropValueMapper foodPropValueMapper;
    @Autowired
    private FoodPropMapper foodPropMapper;
    @Autowired
    private FoodSkuPropValueMapper foodSkuPropValueMapper;
    @Autowired
    private FoodSpuMapper foodSpuMapper;
    @Override
    @GlobalTransactional
    public void insertFoodPropValue(InsertFoodPropValueServiceParam param) {
        FoodProp foodProp = foodPropMapper.selectById(param.getPropId());
        if (foodProp==null) {
            throw new MallException("当前菜品属性不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",param.getStoreId()).eq("spu_id",foodProp.getSpuId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodPropValue> foodPropValueQueryWrapper = new QueryWrapper<>();
        foodPropValueQueryWrapper.eq("prop_value_name",param.getFoodPropValueName()).eq("prop_id",param.getPropId());
        FoodPropValue foodPropValue = foodPropValueMapper.selectOne(foodPropValueQueryWrapper);
        if (foodPropValue!=null) {
            throw new MallException("属性值名称不能重复", CommonResultCodeEnum.INVALID_PARAM);
        }
        FoodPropValue foodPropValueNew = BeanUtil.toBean(param, FoodPropValue.class);
        foodPropValueNew.setPropValueName(param.getFoodPropValueName());
        foodPropValueMapper.insert(foodPropValueNew);
    }

    @Override
    @GlobalTransactional
    public void deleteFoodPropValue(Integer storeId,Integer foodPropValueId) {
        FoodPropValue foodPropValue = foodPropValueMapper.selectById(foodPropValueId);
        if (foodPropValue==null) {
            throw new MallException("当前菜品属性不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        FoodProp foodProp = foodPropMapper.selectById(foodPropValue.getPropId());
        if (foodProp==null) {
            throw new MallException("当前菜品属性不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",storeId).eq("spu_id",foodProp.getSpuId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSkuPropValue> foodSkuPropValueQueryWrapper = new QueryWrapper<>();
        foodSkuPropValueQueryWrapper.eq("prop_value_id",foodPropValueId);
        List<FoodSkuPropValue> foodSkuPropValues = foodSkuPropValueMapper.selectList(foodSkuPropValueQueryWrapper);
        if (foodSkuPropValues.size()!=0) {
            throw new MallException("当前菜品属性还绑定了菜品sku", CommonResultCodeEnum.INVALID_PARAM);
        }
        foodPropValueMapper.deleteById(foodPropValue);
    }

    @Override
    public List<FoodPropValueServiceDto> searchFoodPropValueByFoodPropId(Integer storeId, Integer foodPropId) {
        FoodProp foodProp = foodPropMapper.selectById(foodPropId);
        if (foodProp==null) {
            throw new MallException("当前属性不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",storeId).eq("spu_id",foodProp.getSpuId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodPropValue> foodPropValueQueryWrapper = new QueryWrapper<>();
        foodPropValueQueryWrapper.eq("prop_id",foodPropId);
        List<FoodPropValue> foodPropValues = foodPropValueMapper.selectList(foodPropValueQueryWrapper);
        List<FoodPropValueServiceDto> foodPropValueServiceDtos = BeanUtil.copyToList(foodPropValues, FoodPropValueServiceDto.class);
        return foodPropValueServiceDtos;
    }
}
